package c.x.jy.service.config.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import c.x.jy.common.utils.SmsConfig;
import c.x.jy.dao.entity.BusinessEntity;
import c.x.jy.service.config.BusinessConfigService;
import c.x.jy.service.config.DataService;
import c.x.jy.service.config.WechatConfigService;
import c.x.jy.service.lock.Lock;
import c.x.jy.service.redis.RedisUtils;
import c.x.jy.service.service.BusinessService;
import c.x.jy.service.utils.MemberUtil;
import c.x.jy.service.utils.wechat.WechatCommonUtil;
import c.x.jy.service.utils.wechat.WechatConfig;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class WechatConfigServiceImpl implements WechatConfigService {
	@Autowired
	private BusinessConfigService businessConfigService;
	@Autowired
	private DataService dataService;


	 @Autowired
	 private BusinessService businessService;
	
    private static  final String access_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
    private static  final String  jsapi_ticket_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi";

	@Override
	public WechatConfig getDefultConfing( ) {
		Long bid =null;
		if(MemberUtil.get()!=null&&MemberUtil.get().getBusinessCode()!=null) {
			BusinessEntity b = businessService.selectBusinessByCode(MemberUtil.get().getBusinessCode());
			bid=	b!=null?b.getId():null;
		}
		WechatConfig config = null;
		String v = RedisUtils.redisUtils.get("Business:BusinessConfig:WechatConfigService_getByBusinessId_wechat:"+bid);
		if (v == null) {
			config = new WechatConfig();
			config.setApiKey(businessConfigService.getBusinessConfigValue(bid, "AGENT_XCX_PAYKEYS"));
			config.setAppId(businessConfigService.getBusinessConfigValue(bid, "AGENT_XCX_APPID"));
			config.setAppSecret(businessConfigService.getBusinessConfigValue(bid, "AGENT_XCX_APPSECRET"));
			config.setMchId(businessConfigService.getBusinessConfigValue(bid, "AGENT_XCX_MCHID"));
			config.setGzhAppId(businessConfigService.getBusinessConfigValue(bid, "AGENT_XCX_GZHAPPID"));
			config.setGzhAppSecret(businessConfigService.getBusinessConfigValue(bid, "AGENT_XCX_GZHAPPSECRET"));

			RedisUtils.redisUtils.set("Business:BusinessConfig:WechatConfigService_getByBusinessId_wechat:"+bid, JSON.toJSONString(config));

		} else {
			config = JSONObject.parseObject(v, WechatConfig.class);
		}
		return config; 
	}
	
 
	@Override
	public SmsConfig	getDefultSmsConfing( ) {
		Long bid =null;
		if(MemberUtil.get()!=null&&MemberUtil.get().getBusinessCode()!=null) {
			BusinessEntity b = businessService.selectBusinessByCode(MemberUtil.get().getBusinessCode());
			bid=	b!=null?b.getId():null;
		}
  
		SmsConfig config = null;
		String v = RedisUtils.redisUtils.get("Business:BusinessConfig:WechatConfigService_getByBusinessId_sms:"+bid);
		if (v == null) {
			config = new SmsConfig();
			config.setSmsAccessKeyId(businessConfigService.getBusinessConfigValue(bid, "SmsAccessKeyId"));
			config.setSmsAccessKeySecret(businessConfigService.getBusinessConfigValue(bid, "SmsAccessKeySecret"));
			config.setSmsNotescode(businessConfigService.getBusinessConfigValue(bid, "SmsNotescode"));

			config.setSmsRegcode(businessConfigService.getBusinessConfigValue(bid, "keysmsRegcode"));
			config.setSmsSignName(businessConfigService.getBusinessConfigValue(bid, "keysmsSignName"));
		//	config.setGzhAppId(businessConfigService.getBusinessConfigValue(null, "AGENT_XCX_GZHAPPID"));
		//	config.setGzhAppSecret(businessConfigService.getBusinessConfigValue(null, "AGENT_XCX_GZHAPPSECRET"));
 
			RedisUtils.redisUtils.set("Business:BusinessConfig:WechatConfigService_getByBusinessId_sms:"+bid, JSON.toJSONString(config));

		} else {
			config = JSONObject.parseObject(v, SmsConfig.class);
		}
		return config;
		
		
	};

	@Cacheable(cacheNames="AccessToken",key="'AccessToken_'+#p0", unless="#result == null")
	@Override
	@Lock(key = "#appid")
	public String getAccessToken(String appid, String appsecret) {

	   
		String result = WechatCommonUtil.httpsRequest(String.format(access_token_url, appid,appsecret), "GET", null);
		log.info("获取AccessToken:" + result);
		if (result.indexOf("access_token") < 0) {
			return null;
		}
		Map map = (Map) JSON.parse(result);

		log.info("成功获取AccessToken:" + map);
		return map.get("access_token").toString();
	}

	@Cacheable(cacheNames="AccessToken",key="'JsapiTicket_'+#p0", unless="#result == null")
	@Override
	@Lock(key = "#accesstoken")
	public String getJsapiTicket(String accesstoken ) {
 
		String result = WechatCommonUtil.httpsRequest(String.format(jsapi_ticket_url, accesstoken), "GET", null);
		log.info("获取ticket:" + result);
		if (result.indexOf("ticket") < 0) {
			return null;
		}
		Map map = (Map) JSON.parse(result);

		log.info("成功获取ticket:" + map);
		return map.get("ticket").toString();
	}

 

 
}
